package movie.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import movie.entity.Cinema;
import movie.entity.Film;
import movie.entity.Hall;
import movie.entity.OrderDetail;
import movie.service.CinemaDeleteService;
import movie.service.CinemaScheduleFindAllService;

@Controller
public class CinemaDeleteController {

	@Autowired
	private CinemaScheduleFindAllService cinemaScheduleFindAllService;
	@Autowired
	private CinemaDeleteService CinemaDeleteService;
	
	@RequestMapping(value = "/cinemadelete", method = RequestMethod.GET)
	public String get(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();

		if(session.getAttribute("cinemaId") == null) {
			return "forward:clogin.jsp";
		} 
		
		int movieId = (int) session.getAttribute("movieId");
		
		int cinemaId=(int) session.getAttribute("cinemaId");    
		
		Cinema cinema = new Cinema();
		cinema.setMovieId(movieId);
		cinema.setCinemaId(cinemaId);
		List<Cinema> cinemas=cinemaScheduleFindAllService.findByMovieIdCinemaId(cinema);
		
		for(Cinema cinema11:cinemas)
		{
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			cinema11.setFilmBeginTimeString(format1.format(cinema11.getFilmBeginTime()));
			cinema11.setFilmEndTimeString(format1.format(cinema11.getFilmEndTime()));			
		}
		
		model.addAttribute("cinemas", cinemas);
		return "Cinema/cinemadelete";
	}
	
	@RequestMapping(value = "/cinemadelete", method = RequestMethod.POST)
	public String post(int scheduleId,Model model,HttpServletRequest request) {
		int rows=CinemaDeleteService.delete(scheduleId);
		HttpSession session = request.getSession();
		List<Cinema> cinemas=cinemaScheduleFindAllService.find((int) session.getAttribute("movieId"));
		model.addAttribute("cinemas", cinemas);
		
		//return "Cinema/cinemalist";
		return "redirect:/cinemaFindAllFilm";
	}
	
	@RequestMapping(value = "/filmdeletefind/filmname", method = RequestMethod.GET)
	public String deleteFindGet(String hallName,Model model,HttpServletRequest request)
	{
		HttpSession session = request.getSession();

		if(session.getAttribute("cinemaId") == null) {
			return "forward:clogin.jsp";
		} 
		int movieId = (int) session.getAttribute("movieId");
		int cinemaId=(int) session.getAttribute("cinemaId");
		Hall hall=new Hall();
		hall.setHallName(hallName);
		Cinema cinema=new Cinema();
		cinema.setHall(hall);
		cinema.setMovieId(movieId);
		cinema.setCinemaId(cinemaId);
		if(!hallName.equals(""))
		{
			List<Cinema> cinemas = cinemaScheduleFindAllService.findByMovieIdCinemaIdHallName(cinema);
			for(Cinema cinema11:cinemas)
			{
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				cinema11.setFilmBeginTimeString(format1.format(cinema11.getFilmBeginTime()));
				cinema11.setFilmEndTimeString(format1.format(cinema11.getFilmEndTime()));			
			}
			model.addAttribute("cinemas", cinemas);
			return "Cinema/cinemadelete";
		}
		else 
		{
			List<Cinema> cinemas = cinemaScheduleFindAllService.findByMovieIdCinemaId(cinema);
			for(Cinema cinema11:cinemas)
			{
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				cinema11.setFilmBeginTimeString(format1.format(cinema11.getFilmBeginTime()));
				cinema11.setFilmEndTimeString(format1.format(cinema11.getFilmEndTime()));			
			}
			model.addAttribute("cinemas", cinemas);
			return "Cinema/cinemadelete";
		}
	}
}
